<!DOCTYPE html>
<html style="height: 100%" xmlns="http://www.w3.org/1999/html">
    <head>

        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>理货单打印</title>
        <link rel="shortcut icon" href="/web/static/src/img/favicon.ico" type="image/x-icon">
        <!--<link rel="stylesheet" href="/web/static/src/css/full.css">-->
        <!--<link rel="stylesheet" href="/web/static/lib/fontawesome/css/font-awesome.css">-->
        <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <!--<link rel="stylesheet" href="/web/static/src/css/base.css">-->
        <style type="text/css" id="style">
            body{padding: 10px; font-size: 10pt; overflow: auto}

            @media print
            {
                .print-hide {display:none}
            }

            .td_inline {
                display:block;
                overflow:hidden;
                white-space:nowrap;
                text-overflow:ellipsis;
                width: 370pt;
            }

            .nowrap{
                white-space:nowrap;
            }
            /*.table td.noborder{*/
            /*border-top: 0px;*/
            /*}*/


            h4{margin: 0px;  padding: 0px;font-size: 14pt}

            h5{
                font-size:11pt; font-family:'黑体'; font-weight: bold; margin: 0px;  padding: 0px;
            }

            /*div{font-size: 10pt}*/

            span.foot_sign{border-bottom: 1px solid #ddd}

            /*.table thead tr td{font-weight: bold; font-size: 10pt}*/
            /*.table tbody tr td{border-top: 1px solid #ddd;}*/
            /*.table tbody tr:last-child td{border-top: 1px solid #fff; padding: 0px}*/
            .memo {padding:0px; border-top:0px;}

            .table thead, .table td,  .table th {border-width: 1px;border-style: solid;border-collapse: collapse}

            /*td{font-size:10pt}*/

            .font-small{
                font-size: 8pt;
            }

            .underline{text-decoration: underline;}
        </style>
    </head>

<body>
{%if ids|length == 0 %}
参数错误
{%elif ids|length == 1 %}

    <div class="controls controls-row print-hide">
    <script>
        function LICENSES() {
            document.getElementById('btn-print').innerHTML = "!正在注册打印许可";
            if (window.LODOP){
                LODOP.SET_LICENSES("","CD9A14E608164F55F35D564059E8F43B","C94CEE276DB2187AE6B65D56B3FC2848","15F0BE661E7532B77491843CB2ED09AD");
                batch_print_current();
            }
            else{
                setTimeout(function(){
                    LICENSES();
                }, 100)
            }
        }

        function batch_print_current(){
            document.getElementById('btn-print').innerHTML = "可以开始打印";
            //如果存在父级，则自动打印
            if (parent.batch_print_next){
                document.getElementById('btn-print').innerHTML = "自动打印模式";
                document.getElementById('btn-print').className = "btn btn-primary disabled";
                setTimeout(function(){
                    print_preview();
                    setTimeout(function(){
                        parent.batch_print_next();
                    }, 500)
                }, 500)
            }
        }

        function load_lodop(development){
            var script = document.createElement('script');
            var lodop_host = '192.168.0.10';
            if (development == 'development'){
                lodop_host = '127.0.0.1';
            }
            var result = false;
            script.onload = function(){
                 if ( !result && (!this.readyState || this.readyState == 'complete') ) {
                    result = true;
                    LICENSES();
                }
            }
            script.setAttribute('src', 'http://' + lodop_host + '/CLodopfuncs.js');
            // 把script标签加入head，此时调用开始
            document.getElementsByTagName('head')[0].appendChild(script);
            document.getElementById("btn-print").className = "btn btn-primary";
            document.getElementById("btn-print").innerHTML = "打印理货单";
        }


        function check_print_ok(){

            if (LODOP.CVERSION) LODOP.On_Return = function(TaskID, Value){
                if (Value == '1'){
                    window.print_result_ok = true;
                    document.getElementById("btn-print").className = "btn btn-danger disabled";
                }
                else if (Value == '0'){
                    document.getElementById("btn-print").className = "btn btn-danger disabled";
                    window.print_result_ok = false
                }
                setTimeout(function(){
                    check_print_text();
                }, 100)
            };
            var strResult = LODOP.GET_VALUE('PRINT_STATUS_OK', window.task_name);
        }

        function check_print_text(){
            if (LODOP.CVERSION) LODOP.On_Return = function(TaskID, Value){
                document.getElementById("btn-print").innerHTML = Value;
                setTimeout(function(){
                    check_print_job_exists();
                }, 100)
            };
            var strResult = LODOP.GET_VALUE('PRINT_STATUS_TEXT', window.task_name);
        }

        function check_print_job_exists(){
            if (LODOP.CVERSION) LODOP.On_Return = function(TaskID, Value){
                // 任务不存在
                if (Value == '0'){
                    //任务不存在并且打印成功, 则隐藏打印按钮
                    if (window.print_result_ok){
                        document.getElementById('btn-print').className = "btn btn-success disabled";
                        document.getElementById('btn-print').innerHTML = "打印成功";
                    }
                    //任务不存在并且打印不成功, 重新显示打印按钮
                    else {
                        document.getElementById('btn-print').innerHTML = "重新打印";
                        document.getElementById('btn-print').className = "btn btn-primary";
                    }
                }
                // 如果任务存在则继续检查打印结果
                else if (Value == '1'){
                    setTimeout(function(){
                        get_print_result();
                    }, 300)
                }
            };
            var strResult = LODOP.GET_VALUE('PRINT_STATUS_EXIST', window.task_name);
        }

        function get_print_result(){
            check_print_ok();
        }

        function checkPrintResult(task_name){
            window.task_name = task_name;
            setTimeout(function(){
                get_print_result();
            }, 500)
        }


        function print_preview(){
            if (!LODOP.VERSION) return;
            LODOP.PRINT_INIT("东方团购中心理货单{{order.name}}");
    		LODOP.SET_PRINT_PAGESIZE(1, 0, 2970, "A4"); //A4纸打印

            var printer_count = LODOP.GET_PRINTER_COUNT();
            var intPrinterIndex = -1;
            for (var i = 0; i < printer_count; i ++){
                if(LODOP.GET_PRINTER_NAME(i).indexOf('东方团购中心理货单') >= 0){
                    intPrinterIndex = i;
                    break;
                }
            }
            if (intPrinterIndex == -1){
                alert('东方团购中心理货单打印机不存在，无法打印');
                return;
            }
            CreateOneFormPage1();
            if (LODOP.SET_PRINTER_INDEXA(intPrinterIndex+'')){
                document.getElementById('btn-print').innerHTML = "正在打印";
                document.getElementById('btn-print').className = "btn btn-primary disabled";
                LODOP.SET_PRINT_MODE("CATCH_PRINT_STATUS",true);
                if (LODOP.CVERSION) {
                    LODOP.On_Return = function (task_id, task_name) {
                        if (task_name){
                            checkPrintResult(task_name);
                        }
                        else{
                            document.getElementById('btn-print').innerHTML = '错误:无法获取打印任务'
                        }
                    };
                    LODOP.PRINT();
                    return;
                }
                //LODOP.PRINT();
            }
        }

        function CreateOneFormPage1(){
            var strStyle = "<style>" + document.getElementById("style").innerHTML + "</style>";
            LODOP.ADD_PRINT_TABLE(128,"1%","99%",414,strStyle+document.getElementById("div-body").innerHTML);
            LODOP.SET_PRINT_STYLEA(0,"Vorient",3);
            LODOP.SET_PRINT_STYLEA(0,"FontSize",8);

            LODOP.ADD_PRINT_HTM(20, 0,"60%",109,document.getElementById("div-title").innerHTML);
            LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
            LODOP.SET_PRINT_STYLEA(0,"LinkedItem",1);

            LODOP.ADD_PRINT_HTM(65,"5%","90%",109,document.getElementById("div-header").innerHTML);
            LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
            LODOP.SET_PRINT_STYLEA(0,"LinkedItem",1);

            LODOP.ADD_PRINT_HTM(544,"5%","90%",54,document.getElementById("div-note").innerHTML);
            LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
            LODOP.SET_PRINT_STYLEA(0,"LinkedItem",1);
            LODOP.SET_PRINT_STYLEA(0,"FontSize",8);
            LODOP.SET_PRINT_STYLEA(0,"Horient",1);

            LODOP.ADD_PRINT_HTM(1070, 30, 450, 20,document.getElementById("div-foot").innerHTML);
            LODOP.SET_PRINT_STYLEA(0,"ItemType",1);

            LODOP.ADD_PRINT_HTM(1070, 500, 150,30,"<span tdata='pageNO' style='font-size:12px'>第##页</span>/<span tdata='pageCount'  style='font-size:12px'>共##页</span>");
            LODOP.SET_PRINT_STYLEA(0,"ItemType",1);


            LODOP.ADD_PRINT_BARCODE(2, 530, 230, 60, "Code93", "{% if order.real_so_id %}{{ order.real_so_id.name }}{% else %}{{ order.name }}{% endif%}");
            LODOP.ADD_PRINT_BARCODE(950, 630, 150, 150,"QRCode","{{url_prefix}}/wx/stock/tally/{% if order.real_so_id %}{{ order.real_so_id.id }}{% else %}{{ order.id }}{% endif%}");

            LODOP.ADD_PRINT_TEXT(1075, 640, 150, 20,"扫码打印出库单");
            LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
            LODOP.SET_PRINT_STYLEA(0,"FontSize",10);
        };

    </script>
        <a id="btn-print" href="javascript:print_preview()" class="btn btn-primary" data-original-text="">直接打印</a>
        注: 点击按钮直接打印默认打印机理货单;<br />
        选择打印机Ctrl+P键，或者菜单上面选择文件->打印; <br />
        文件->页面设置里面可以去除页眉页脚

    </div>

<div id="div-title">
    <table border="0" cellSpacing="0" cellPadding="0" width="100%">
        <tbody>
            <tr>
                <td align="center">
                    <h4 class="page_head">
                        {% if have_fake_order or is_fake_order %} （必须本人签收） {% endif %}
                        {% if order.is_return %}
                            销售退货单
                        {% elif order.real_so_id %}
                            销售F 理货单
                        {% else %}
                            {% if order.is_exchange %}
                                销售换货
                            {% else %}
                                {% if order.state in ['draft','sent'] %}报价单{% else %}销售出库{% endif %}
                            {% endif %}
                            理货单
                        {% endif %}
                        {% if order.real_so_id %}{{ order.real_so_id.name }}{% else %}{{ order.name }}{% endif%}
                    </h4>
                </td>
            </tr>
    </tbody>
</table>
<!--{% if order.real_so_id %}-->
    <!--{{ order.real_so_id.name }}-->
<!--{% else %}-->
    <!--{{ order.name }}-->
<!--{% endif%}-->
</div>
<div id="div-header">
    <table border="0" cellSpacing="0" cellPadding="0" width="100%" style="font-size:12px">
        <tbody>
            <tr>
                <td width="40%">客户:
                    {% if order.partner_id.is_company %}
                        {{ order.partner_id.name|default('', True) }}
                    {% else %}
                        {{ order.partner_id.name|default('', True) }}{{ order.partner_id.parent_id.name|default('', True) }}
                    {% endif %}
                    <!--联系人:-->
                    <!--，{{ order.consignee}}-->
                </td>
                <td width="33%">
                    下单日期: {{ order.date_order|default(order.create_date,True)|strftime('%Y-%m-%d') }}
                </td>
                <td><h4>
                    {% if order.is_return %}
                    计划退货:
                    {% else %}
                    计划发货:
                    {% endif %}
                    {{ order.expected_delivery_time }}</h4>
                </td>
            </tr>
            <tr>
                <td width="40%" colspan="2">地址:
                    {% if order.province_id and order.province_id.name != '上海市' %}
                    {{order.province_id.name}}
                    {% endif %}
                     &nbsp;
                    {% if order.city_id %}
                    {{order.city_id.city_name}}
                    {% endif %}
                     &nbsp;
                    {% if order.county_id %}
                    {{order.county_id.county_name}}
                    {% endif %}
                     &nbsp;
                        {% if order.street %}
                    {{order.street}}
                    {% endif %}

                    ，送货方式:
                    {% if order.shipping_type_sale == 'express_delivery' %}
                    快递
                    {% endif %}
                    {% if order.shipping_type_sale == 'sale_delivery' %}
                    销售送货
                    {% endif %}
                    {% if order.shipping_type_sale == 'self_logistics' %}
                    公司物流
                    {% endif %}
                    {% if order.shipping_type_sale == 'direct_delivery' %}
                    供应商直送
                    {% endif %}
                </td>
                <td>
                    {% if order.expected_delivery_time %}
                        具体时间: {{ order.expected_delivery_hour }}
                    {% else %}
                        未指定
                    {% endif %}
                </td>
            </tr>
        </tbody>
    </table>
</div>


<div id="div-body">
    <table class="table table-striped" width="100%" style="font-size:12px">
        <thead>
            <tr>
                <td width="15">#</td>
                <td>产品名称</td>
                <td>产品型号</td>
                <td>销售备注</td>
                <td class="nowrap" style="text-align: right;">订单数量</td>
                <td>是否备齐</td>
                <td>分批数量</td>
            </tr>
        </thead>
        <tbody>
            {% for line in order.order_line %}
            <tr>
                <td>{{loop.index}}</td>
                <td>
                        <span>{{ line.name}}</span>
                </td>
                <td>{{line.product_id.prod_model|default("", True)}}</td>
                <td>{{line.prod_spec|default("", True)}}{{line.sale_note|default("", True)}}</td>
                <td style="text-align: right;">{{ line.product_uom_qty|int() }} {% if line.product_uos %}({{ line.product_uos.name }}){% endif %}</td>
                <td>☐</td>
                <td></td>
            </tr>
				{% for order_card in order.sale_order_id %}
						{% if order_card.product_id.id==line.product_id.id %}
						<tr>
							<td></td>
							<td style="text-align: left;" colspan="7">
							券号:{{order_card.starting_number.code}}{% if order_card.continue_number!=1 %}~{{order_card.ending_number}}({{order_card.continue_number}}张){% else %} ({{order_card.continue_number}}张){% endif %}
							</td>
						</tr>
						{% endif %}
				{% endfor %}
            {% endfor %}

            {% if official_invoice_map.get(False) or official_invoice_map.get(True) %}
            <tr><td colspan="8">
                {% if official_invoice_map.get(False) %}开票:{{official_invoice_map.get(False)}}张{% endif %}
                {% if official_invoice_map.get(True) %}开票:{{official_invoice_map.get(True)}}张(W){% endif %}
            </td></tr>

            {% endif %}
        </tbody>
    </table>
</div>
    <div style="float:right; margin-top:-20px">
        <img src="/wx/work/qr_or_bar_code?type=QR&width=100&value=stock/tally/{% if order.real_so_id %}{{ order.real_so_id.id }}{% else %}{{ order.id }}{% endif%}" width="100" height="100" />
    </div>
<div id="div-note">
    <div class="memo" style="font-size:13px; font-weight: bold">
        {{order.note|default('', True)|replace('\n', '<br/>')|safe}}
        {% if order.stock_note %}
        仓库备注: {{order.stock_note}}
        {% endif %}
		<br/>
		{% if order.partner_id.stock_auto_note %}
		{{order.partner_id.stock_auto_note}}
		{% endif %}
    </div>
    <div style="padding-top:50px;padding-left:15px;padding-bottom:20px">
            包装箱（袋）类型:_______________, 共: ______________箱(袋), 每箱里面产品数量共:____________(袋 件 个 套)
    </div>
</div>

<div id="div-foot">
    <div style="font-size:12px" class="col-md-8">
    业务：<span class="foot_sign">{{ order.user_id.name|default("　　　　") }}</span>
    下单: <span class="foot_sign">{{ order.create_uid.name|default("　　　　") }}</span>
    财务：<span class="foot_sign">　　　　　　</span>
    采购：<span class="foot_sign">　　　　　　</span>
    仓库：<span class="foot_sign">　　　　　　</span>
    </div>
</div>

{%elif ids|length > 1 %}
        <a id="btn-batch-print" href="javascript:batch_print_next()" class="btn btn-primary disabled">网络错误 - 无法打印</a>
        注: 一共{{ids|length}}单，请确认打印机是否有足够的空白纸<br />
        <iframe id="single_print_iframe"
                width="100%" height="400px" style="margin-top:10px;border: 1px solid;" scrollin="yes"></iframe>
    <script language="javascript">
        var ids = {{ids}};
        var current_ids_index = 0;

//        function isIe(){
//            return window.ActiveXObject ? true : false;
//        }
//        function iframe_onload(){
//            //alert('iframe onload');
//            var single_print_iframe = document.getElementById('single_print_iframe');
//            window.current_ids_index ++;
//        }
//        window.onload = function(){
//            var single_print_iframe = document.getElementById('single_print_iframe');
//            if ( isIe() ) {
//                single_print_iframe.attachEvent( "onload", iframe_onload );
//            } else {
//                single_print_iframe.onload = iframe_onload;
//            }
//        }

        function batch_print_next(){
            document.getElementById('btn-batch-print').innerHTML = "正在打印第" + (window.current_ids_index + 1) + "单";
            document.getElementById('btn-batch-print').className = "btn btn-primary disabled";
            if (window.current_ids_index < ids.length){
                var single_print_iframe = document.getElementById('single_print_iframe');
                var url = window.location.href;
                right_pos = url.lastIndexOf('/');
                url = url.substring(0, right_pos + 1);
                var order_id = ids[current_ids_index];
                //alert(order_id);
                single_print_iframe.src = url + order_id;
                window.current_ids_index ++;
            }
            else{
                document.getElementById('btn-batch-print').innerHTML = "已经全部打印完毕";
                document.getElementById('btn-batch-print').className = "btn btn-primary disabled";
            }
        }

        function load_lodop(development){
            var script = document.createElement('script');
            var lodop_host = '192.168.0.10';
            if (development == 'development'){
                lodop_host = '127.0.0.1';
            }
            script.setAttribute('src', 'http://' + lodop_host + '/CLodopfuncs.js');
            // 把script标签加入head，此时调用开始
            document.getElementsByTagName('head')[0].appendChild(script);
            document.getElementById("btn-batch-print").className = "btn btn-primary";
            document.getElementById("btn-batch-print").innerHTML = "开始批量打印";
        }
    </script>
{%endif%}
    <script src="http://{{lodop_host}}/jsonp_lodop.js"></script>
    </body>
</html>
